package com.oa.role.BizImpl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.oa.entity.TblRight;
import com.oa.entity.TblRole;
import com.oa.role.Biz.RoleBiz;
import com.oa.role.Dao.RoleDao;

public class RoleBizImpl implements RoleBiz{
	
	private RoleDao roleDao;
	
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	/*
	 * 角色加载
	 * @see com.oa.role.Biz.RoleBiz#load(java.lang.Integer)
	 */
	@Override
	public TblRole load(Integer Id) {
		// TODO Auto-generated method stub
		return this.roleDao.load(Id);
	}
	/*
	 * 角色增加
	 * @see com.oa.role.Biz.RoleBiz#add(com.oa.entity.TblRole)
	 */
	@Override
	public boolean add(TblRole role) {
		// TODO Auto-generated method stub
		return this.roleDao.add(role);
	}
	/*
	 * 角色删除
	 * @see com.oa.role.Biz.RoleBiz#delete(com.oa.entity.TblRole)
	 */
	@Override
	public boolean delete(TblRole role) {
		// TODO Auto-generated method stub
		return this.roleDao.delete(role);
	}
	/*
	 * 角色更新
	 * @see com.oa.role.Biz.RoleBiz#update(com.oa.entity.TblRole)
	 */
	@Override
	public boolean update(TblRole role) {
		// TODO Auto-generated method stub
		return this.roleDao.update(role);
	}
	/*
	 * 数据查询
	 * @see com.oa.role.Biz.RoleBiz#searchData(java.lang.String, java.util.Map)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<TblRole> searchData(String hql, Map params) {
		// TODO Auto-generated method stub
		List<TblRole> rlist = this.roleDao.searchData(hql, params);
		return rlist;
	}
	/*
	 * 分配权限
	 * @see com.oa.role.Biz.RoleBiz#Role_Assgin_Rights()
	 */
	@Override
	public boolean Role_Assgin_Rights(Integer roleId,String[] rightsCode_box) {
		// TODO Auto-generated method stub
		boolean flag = true;
		TblRole tblRole = this.load(roleId);
		Set<TblRight> rightSet = tblRole.getRights_box();
		try{
			if(rightSet.size()>0){
				//清空原有权限
				tblRole.getRights_box().clear();
			}
			TblRight tempTR = new TblRight();
			rightSet = new HashSet<TblRight>();
			for(String s:rightsCode_box){
				tempTR = this.load(s);
				rightSet.add(tempTR);
			}
			System.out.println("权限个数："+rightSet.size());
			//更新·保存权限
			tblRole.setRights_box(rightSet);
			
			this.add(tblRole);
		}catch(Exception e){
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 删除权限
	 * @see com.oa.role.Biz.RoleBiz#Role_Delete_Rights()
	 */
	@Override
	public boolean Role_Delete_Rights() {
		// TODO Auto-generated method stub
		return false;
	}
	/*
	 * 加载指定权限
	 * @see com.oa.role.Biz.RoleBiz#load(java.lang.String)
	 */
	@Override
	public TblRight load(String rights_code) {
		// TODO Auto-generated method stub
		return this.roleDao.load(rights_code);
	}
}
